package tt.dz.dao.ext.system;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;

import tt.dz.dao.base.BasisDao;
import tt.dz.entity.exception.OmsDaoException;
import tt.dz.entity.system.OmsRoleAuth;
import tt.dz.entity.system.OmsUrlList;


/**
 * 角色授权数据操作
 * @author Administrator
 * @date 2015年12月16日
 */
@Repository
public class OmsRoleAuthDao extends BasisDao<OmsRoleAuth>  {

	/**
	 * 查询角色所有的有效授权信息
	 * @author Administrator
	 * @param role_id
	 * @return
	 */
	public List<OmsRoleAuth> findInvalidRoleAuthByRoleIdEmbedList(String role_id){
		
		OmsRoleAuth roleAuth = new OmsRoleAuth();
		//设置查询参数
		roleAuth.setRole_id(role_id);
		roleAuth.setValid_flag(1);
		return super.findExample(roleAuth, null, null, null, null);
	}
	
	/**
	 * 查询角色所有的有效授权信息多roleId
	 * @author Administrator
	 * @param role_id
	 * @return
	 */
	public List<OmsRoleAuth> findInvalidRoleAuthByRoleIdsEmbedList(String role_ids){
		
		return super.findDataBySqlOfClass("SELECT role_id,url_id,create_time,create_oper_id,update_time,update_oper_id,valid_flag FROM oms_role_auth "
				+ "where role_id in (?) and valid_flag=?", OmsRoleAuth.class, role_ids, 1);
	}
	
	
	/**
	 * 逻辑删除授权信息
	 * @author Administrator
	 * @param role_id
	 * @param url_id
	 * @return
	 */
	public int logicDeleteRoleAuthByRoleIdAndUrlId(String role_id,String url_id){

		OmsRoleAuth roleAuth = new OmsRoleAuth();
		//设置主键列
		roleAuth.setRole_id(role_id);
		roleAuth.setUrl_id(url_id);
		
		return super.LogicDeleteObject(roleAuth);
	}
	
	public void deleteAuthIds(String role_id) throws OmsDaoException
	{
		try
		{
			String sql = "delete from oms_role_auth where role_id = ?";
			this.getJdbcTemplate().update(sql, role_id);
		}
		catch(Exception e)
		{
			throw new OmsDaoException(e);
		}
	}

	
	public void addOmsRoleAuths(final String[] url_ids,final String role_id) throws OmsDaoException
	{
		try
		{
//			List<String> list = new ArrayList<String>();
			String sql = "insert into oms_role_auth(role_id,url_id,valid_flag,create_time,update_time,create_oper_id,update_oper_id) values(?,?,1,now(),now(),'-1','-1')";
			this.getJdbcTemplate().batchUpdate(sql,new BatchPreparedStatementSetter() 
			{
				
				@Override
				public void setValues(PreparedStatement arg0, int arg1) throws SQLException 
				{
					int index = 1;
					arg0.setString(index++, role_id);
					arg0.setString(index++, url_ids[arg1]);
				}
				
				@Override
				public int getBatchSize() {
					return url_ids.length;
				}
			});
		}
		catch(Exception e)
		{
			throw new OmsDaoException(e);
		}
	}
	
}